package com.winchain.chainbill.bill.biz.transfer.rx;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillXfeAssetSubscriber extends OnErrorNotImplementedSubscriber<BillEvent<CbXfe>> {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public void onApplicationEvent(BillEvent<CbXfe> t) {
		CbXfe cbXfe = (CbXfe) t.getBill().getSource();
		BigDecimal xfeAmt = new BigDecimal(0);
		if ("true".equals(t.getValue("splitXfe"))) {
			cbXfe = (CbXfe) t.getValue("cbXfe");
			xfeAmt = (BigDecimal) t.getValue("xfeAmt");
		} else {
			xfeAmt = cbXfe.getBillAmt();
		}
		String bizType = "cb_xfe_apy";
		String optType = "qt_blk";
		AcctTxRec tx = getTxRec(cbXfe, xfeAmt, bizType, optType);

		tx.setAcctin(false);
		this.tradeService.callback(tx);
	}

	private AcctTxRec getTxRec(CbXfe cbXfe, BigDecimal xfeAmt, String bizType, String optType) {
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());
		txRec.setTxAmt(xfeAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs("��������������������");
		txRec.setTxType(bizType);
		txRec.setOptType(optType);
		txRec.setTxSts("S");
		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(cbXfe.getBillId());
		txRec.setDataScLvl("B");
		txRec.setCurrCode(AssetCode.XCB.name());
		txRec.setTxChnCode("C");
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");
		txRec.setTxOperator(CommonDataUtil.getCurEntId());

		AcctBas acct = this.billAcctService.getAcctBas(cbXfe.getBuyerId(), "1", null);
		txRec.setTxAcctNo(acct.getAcctNo());
		txRec.setTxAcctNm(acct.getAcctNm());
		txRec.setTxBankNm(acct.getFiNm());
		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getTxAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setTxBkpId(txAcctBkp.getBkpId());

		return txRec;
	}
}
